<?php
class organization_operation{
    // 应用实例对象
    static private $app='organization';

    // 模型实例
    static private $model;
    
    // 外部可调用的地区数组
    public $regions;
    
    //组织结构
    public $_org_html    = '';
    
    // 构造方法
    public function __construct($app){
        if(!isset(self::$model)){
            self::$model = app::get(self::$app)->model('organization');
        }
    }

    /**
     * 得到组织信息 
     */
    public function getGropById($orgId='',$orgType=''){
        if ($orgId){
            $base_filter = array('parent_id' => $orgId,'del_mark' => 0);
        }else{
            $base_filter = array('org_level_num' => '1','del_mark' => 0);
        }

        if($orgType){
            $filter = array_merge($base_filter,array('org_type'=>$orgType));
            $aTemp = self::$model->getList('org_id,org_no,org_type,org_name,status,org_level_num', $filter, 0, -1, 'org_id ASC');
        }else{
            //门店管理页面重新获取$base_filter 显示启用的组织层级和相应的所属门店
            if($orgId){
                $base_filter = "parent_id=".$orgId;
            }else{
                //页面首次加载 显示最高组织层级
                $base_filter = "org_level_num=1";
            }
            $base_filter = $base_filter." and del_mark=0";
            $aTemp = self::$model->db->select("select org_id,org_no,org_type,org_name,status,org_level_num from ".DB_PREFIX."organization_organization where ".$base_filter." and (org_type=2 or (org_type=1 and status=1)) order by org_id ASC");
        }
        
        if (is_array($aTemp)&&count($aTemp) > 0){
            foreach($aTemp as $key => $val){
                $aTemp[$key]['status']=intval($val['status']);
                $aTemp[$key]['child_count'] = $this->getChildCount($val['org_id'],$orgType);
                $aTemp[$key]['step'] = intval($val['org_level_num'])-1;
            }
        }
        return $aTemp;
    }
    
    /**
     * 获取指定的下级组织数量
     */
    private function getChildCount($org_id, $org_type=''){
        if($org_type){
            $filter = array('parent_id' => $org_id,'del_mark' => 0,'org_type'=>$org_type);
            $cnt = self::$model->count($filter);
        }else{
            //门店管理页面显示启用的组织层级和相应的所属门店
            $cnt = self::$model->db->count("select count(*) from ".DB_PREFIX."organization_organization where parent_id=".intval($org_id)." and del_mark=0 and (org_type=2 or (org_type=1 and status=1))");
        }
        return $cnt;
    }

    
    function getShowData($orgId)
    {
        $this->getAllChildNode($orgId);
        
        if($this->_data)
        {
            $this->formatChildNode($orgId);
        }
        
        
        
        return $this->_data;
    }
    
    /**
     * 获取所有下级组织
     * 
     * @param $orgId intval
     * @return Array
     */
    function getAllChildNode($orgId)
    {
        $data      = array();
        $filter    = array('parent_id' => $orgId, 'org_type'=>1, 'del_mark' =>0);
        $dataList  = self::$model->getList('org_id, org_no, org_name, status, org_level_num', $filter, 0, -1, 'org_id ASC');
        
        if($dataList)
        {
            while($rows = array_shift($dataList))
            {
                $rows['items']    = $this->getAllChildNode($rows['org_id']);
                
                $rows['child_count']    = count($rows['items']);//下级组织数量
                $rows['step']           = intval($rows['org_level_num'])-1;
                
                $data[]           = $rows;
            }
        }
        
        return $data;
    }
    
    /**
     * 格式化html代码展示所有下级组织
     * 
     * @param Array $data
     * @return string
     */
    function getAllChildNodeHtml($data)
    {
        if($data)
        {
            foreach ($data as $key => $val)
            {
                $this->_org_html    .= $this->formatHtml($val);
                
                //递归
                if($val['items'])
                {
                    $this->getAllChildNodeHtml($val['items']);
                }
                
            }
        }
        
        return $this->_org_html;
    }
    
    function formatHtml($val)
    {
        $html    = '<tr parentid="'. $val['org_id'] .'" class="provice-bg">';
        
        $html    .= '<td style="text-align:left; width:320px;"><div style="padding-left:'. ($val['step'] * 25) .'px">';
        if($val['child_count'] > 0)
        {
            $html    .= '<span class="imgTree tree_open" id="'. $val['org_id'] .'"> &nbsp;&nbsp; </span>';
        }
        else
        {
            $html    .= '<span class="imgTree tree_open"> &nbsp;&nbsp; </span>';
        }
        $html    .= '<span style="font-weight:700; color:#000; text-decoration:none;padding-right:15px;" >'. $val['org_name'] .'</span></div></td>';
        
        $html    .= '<td style="width:60px;"><span>'. ($val['status'] == 1 ? '启用' : '停用') .'</span></td>';
        
        $html    .= '<td style="width:350px">';
        if($val['status'] == 1)
        {
            $html    .= '<a href="javascript:if(confirm(\'确认停用该组织？\')){W.page(\'index.php?app=organization&ctl=admin_management&act=doUnactiveGropItem&org_id='. $val['org_id'] .'\', $extend({method: \'get\'}, JSON.decode({})), this);setTimeout(\'location.reload()\',500);}void(0);" target="">停用</a>';
            
            if($val['org_level_num'] < 5)
            {
                $html    .= '&nbsp;|&nbsp;<a class="i" href=\'index.php?app=organization&ctl=admin_management&act=addChildGropItem&org_id='. $val['org_id'] .'\' target="dialog::{title:\'<{t}>添加下级组织架构 <{/t}>\',width:800,height:300}">添加下级</a>';
            }
        }
        else
        {
            $html    .= '<a href="javascript:W.page(\'index.php?app=organization&ctl=admin_management&act=doActiveGropItem&org_id='. $val['org_id'] .'\', $extend({method: \'get\'}, JSON.decode({})), this);setTimeout(\'location.reload()\',500);void(0);" target="">启用</a>';
        }
        $html    .= '&nbsp;|&nbsp;<a class="i" href=\'index.php?app=organization&ctl=admin_management&act=editGropItem&org_id='. $val['org_id'] .'&from_page_act=org_show\' target="dialog::{title:\'<{t}>组织架构 <{/t}>\',width:800,height:300}">编辑</a>';
        $html    .= '&nbsp;|&nbsp;<a href="javascript:if(confirm(\'确认删除该组织？\')){W.page(\'index.php?app=organization&ctl=admin_management&act=doDelGropItem&org_id='. $val['org_id'] .'\', $extend({method: \'get\'}, JSON.decode({})), this);setTimeout(\'location.reload()\',500);}void(0);" target="">删除</a>';
        $html    .= '</td>';
        
        $html    .= '<td></td></tr>';
        
        return $html;
    }
}
